package net.jxtz.jxtzos.landtax;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.github.shyiko.mysql.binlog.event.EventData;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import net.jxtz.jxtzos.entity.landtax.Residential;
import net.jxtz.jxtzos.entity.landtax.dto.ListHeader;
import org.apache.poi.ss.formula.functions.T;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 针对 land tax 模块的binlog文件分析
 * @author a123
 */

@Slf4j
public class BinLogAnalysis {

    /**
     * 装载单个Residential对象
     * @param serializes binlog数据
     * @return 装载好的Residential对象
     */
    @SneakyThrows
    public static Residential loadObject(Serializable[] serializes){
        Field[] declaredFields = Residential.class.getDeclaredFields();
        Residential residential = new Residential();
        for (int i = 0; i < serializes.length; i++) {
            Field field = declaredFields[i];
            Serializable serialize = serializes[i];
            String name = field.getName();
            log.info(serialize.toString() + " ==> " + name);
            loadField(residential, field, serialize);
        }
        log.info(residential.toString());
        return residential;
    }

    /**
     * 批量装载Residential对象
     * @param list binlog数据集合
     * @return Residential集合
     */
    public static List<Residential> loadObject(List<Serializable []> list){
        List<Residential> residentials = new ArrayList<>();
        for (Serializable[] serializables : list) {
            residentials.add(loadObject(serializables));
        }
        return residentials;
    }

    /**
     * 属性装载
     * @param residential Residential对象，需要提前创建好再传进来
     * @param field 属性对象
     * @param serializable binlog单个属性数据
     */
    @SneakyThrows
    private static void loadField(Residential residential, Field field, Serializable serializable){
        field.setAccessible(true);
        String s = serializable.toString();
        String type = field.getType().getTypeName().replace("java.lang.", "");
        switch (type){
            case "String":
                field.set(residential, s);
                break;
            case "Long":
                field.set(residential, Long.valueOf(s));
                break;
            case "Integer":
                field.set(residential, Integer.valueOf(s));
                break;
            case "Short":
                field.set(residential, Short.valueOf(s));
                break;
            case "Float":
                field.set(residential, Float.valueOf(s));
                break;
            case "Double":
                field.set(residential, Double.valueOf(s));
                break;
            default:
        }

    }
}
